Thread: c[-1]?

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,401
    Quote Originally Posted by Tanuj_Tanmay
    But if store some value there...it will print it
    Maybe, even probably. It might also reformat your hard disk drive or attempt to launch a nuke targeted at your location. Undefined behaviour, friend.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Use the FAQ Luke!


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #18
    Registered User
    Join Date
    Jun 2005
    Posts
    6,814
    Quote Originally Posted by Bladactania View Post
    I don't think that is considered undefined behavior. c[-1] returns the "value" stored in the address that is one before c[0].

    Although, I guess that if c[0] is stored on address "0" or the lowest possible address, then c[-1] would generate an error. Or of c[0] is one address after the end of system protected memory.

    I append my original statement. c[-1] yields undefined behavior.
    It yields undefined behaviour, definitely, because the C standard says so.

    Section J.2 of the 1999 C standard summarises many variants of undefined behaviour, one of which is;
    An array subscript is out of range, even if an object is apparently accessible with the given subscript (as in the lvalue expression a[1][7] given the declaration int a[4][5]) (6.5.6).
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #19
    Registered User
    Join Date
    Apr 2009
    Posts
    13
    the reason why "someone" included the terminology of "bad programming practices" is because they are bad programming practices, hence, should be avoided. I see no reason to hold on to a very buggy c[-1] let alone to defend its use.

  5. #20
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by charlitos View Post
    the reason why "someone" included the terminology of "bad programming practices" is because they are bad programming practices, hence, should be avoided. I see no reason to hold on to a very buggy c[-1] let alone to defend its use.
    I agree, it should be used with extreme care. But how do you propose we'd solve the problem of malloc that stores a block of data stored BEFORE the actual pay-load data returned, and when we come to free, we need to find that block of data? Of course, we could have some sort of list of allocations and search that list for the passed in pointer. But some people certainly use some form like this:
    Code:
    void free(void *p)
    {
    ...
        AdminBlock *pAdmin = ((AdminBlock *)p)-1;
    ...
    Which is about the same as the c[-1] described above.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #21
    Registered User
    Join Date
    Jun 2005
    Posts
    6,814
    Quote Originally Posted by matsp View Post
    Code:
    void free(void *p)
    {
    ...
        AdminBlock *pAdmin = ((AdminBlock *)p)-1;
    ...
    Which is about the same as the c[-1] described above.
    Well, it's not actually.

    Firstly, it is up the the implementation as to how it implements malloc() and free(). The purpose of implementation defined behaviour is to allow freedom for the implementation (the compiler, standard library, etc) .... the implementation can attach any meaning to undefined behaviour that makes sense to it.

    Second, if free() behaves like you describe then malloc() and friends presumably do something like;
    Code:
    void *malloc(size_t n)
    {
          AdminBlock *buffer = (AdminBlock *)get_memory_from_system(n);
          return (void *)(buffer + 1);
    }
    That addition of the offset (+1) in malloc() would ensure the negative offset (-1) within free() yields a valid address within the dynamically allocated memory/array. So no need to rely on undefined behaviour.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  7. #22
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Grumpy, yes of course, we need a matching pair where malloc allocates a bit of extra memory, and then gives the address of something WIHTIN that block of memory, with enough space for the admin block BEFORE.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Promblem with code
    By watchdogger in forum C Programming
    Replies: 18
    Last Post: 01-31-2009, 06:36 PM
  2. Replies: 13
    Last Post: 09-24-2008, 06:16 PM
  3. string manipulation problem
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 10-06-2005, 06:13 AM
  4. Bubble sort
    By Lionmane in forum C Programming
    Replies: 5
    Last Post: 07-09-2005, 11:30 AM
  5. Need help to understand this STL code.
    By Hulag in forum C++ Programming
    Replies: 3
    Last Post: 04-26-2005, 01:59 PM